#include "complexe.h"

complexe::complexe(const QString & s):std::complex<double>(0,0){
    double a = 1, b = 1;
    int mid = s.indexOf("$");
    if(mid == -1)
        a = s.toDouble();
    else{
        a = QString(s).remove(mid,s.count()).toDouble();
        b = QString(s).remove(0,mid+1).toDouble();
    }
    setReal(a);
    setImag(b);
    p_re = len(a);
    p_im = len(b);
}

QString& complexe::toQString() const{
    QString *s = new QString();
    *s = QString().setNum(real(),'g',p_re) + " $ " + QString().setNum(imag(),'g',p_im);
    return *s;
}

void complexe::setP_Re(const int p){
    p_re = p;
}

void complexe::setP_Im(const int p){
    p_im = p;
}

void complexe::setReal(const double a){
    complexe A(a-real(),0);
    this->operator +=(A);
}

void complexe::setImag(const double b){
    complexe B(0,b-imag());
    this->operator +=(B);
}

bool complexe::isNull() const{
    return (imag()==0 && real()==0);
}

QString toQString(const complexe x){
    return x.toQString();
}
